﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace NetUtilityLib
{
    class Config
    {
        public static string GetConnectionStringsConfig(string connectionName)
        {
            //指定config文件读取
            string file = System.Windows.Forms.Application.ExecutablePath;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            string connectionString =
                config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();
            return connectionString;
        }

        public static string GetConnStr(string connectionName)
        {
            return ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();
        }

        public static string GetAppStr(string name)
        {
            return ConfigurationManager.AppSettings[name];
        }

        public static string GetSelectionStr(string name)
        {
            Object obiect = ConfigurationManager.GetSection("fileCopyTool");
            Object obiect1 = ConfigurationManager.GetSection("dms");
            return "";
        }

        public static void SetAppStr(string name, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[name] != null)
                config.AppSettings.Settings[name].Value = value;
            else
            {
                config.AppSettings.Settings.Add(name, value);
            }
            
            config.Save(ConfigurationSaveMode.Modified);
            //重新加载新的配置文件
            //ConfigurationManager.RefreshSection("appSettings");
        }

        public static void Refresh()
        {
            //重新加载新的配置文件
            ConfigurationManager.RefreshSection("appSettings");
        }

    }
}
